package dbdao;

import java.sql.*;
import java.util.*;
import items.*;

public class DBaseManager 
{
	private Connection con;
	private String url;
	private String dbname;
	private String user;
	private String passwd;
	
	public DBaseManager()
	{
		url = "mejorcalidadprecio.com";
		dbname = "calprec";
		user = "user";
		passwd = "passwd";
	}
	
	public DBaseManager( String user , String passwd )
	{
		url = "mejorcalidadprecio.com";
		dbname = "calprec";
		this.user = user;
		this.passwd = passwd;
	}
	
	public DBaseManager( String url , String dbname , String user , String passwd )
	{
		this.url = url;
		this.dbname = dbname;
		this.user = user;
		this.passwd = passwd;
	}
	
	public ArrayList<LString> getElements( String sql )
	{
		ArrayList<LString> aL = new ArrayList<LString>();
		try
		{
			connect();
			Statement stmt = con.createStatement();
			ResultSet rS = stmt.executeQuery(sql);
			while( rS.next() )
			{
				LString dbr = new LString();
				boolean t = true;
				int i=1;
				while( t )
				{
					try
					{
						String str = rS.getString(i);
						dbr.add(str);
					}
					catch(Exception e)
					{
						t = false;
					}
					i++;
				}
				aL.add(dbr);
			}
			disconnect();
		}
		catch( Exception e )
		{
			System.out.println("No se pudo completar el acceso a la base de datos:");
			e.printStackTrace();
		}
		return aL;
	}
	
	public void insertElements( ArrayList<LString>ldbr , String tName )
	{
		connect();
		for( int i=0 ; i<ldbr.size() ; i++ )
		{
			String sql = "INSERT INTO " + tName + " VALUES(";
			LString dbr = ldbr.get(i);
			if( dbr.length>1 )
				sql+=dbr.get(0);
			for( int j=1 ; j<dbr.length ; j++ )
				sql+="," + dbr.get(j);
			sql += ")";
			try
			{
				Statement stmt = con.createStatement();
				stmt.executeUpdate(sql);
			}
			catch( Exception e )
			{
				System.out.println("Error insertando filas, en fila numero " + i);
				e.printStackTrace();
			}
		}
		disconnect();
	}

	public void updateElements( ArrayList<String> lSQL )
	{
		int i=0;
		try
		{
			connect();
			Statement stmt = con.createStatement();
			for( i=0 ; i<lSQL.size() ; i++ )
				stmt.executeUpdate(lSQL.get(i));
		}
		catch( Exception e )
		{
			System.out.println("Error actualizando la base de datos, en la fila " + i);
			e.printStackTrace();
		}
	}
	
	public int getLastIndex( String table )
	{
		int index = -1;
		String sql = "SELECT * FROM " + table + " ORDER BY id DESC LIMIT 0,1";
		try
		{
			connect();
			Statement stmt = con.createStatement();
			ResultSet rS = stmt.executeQuery(sql);
			if( rS.next() )
				index = rS.getInt("ID");
		}
		catch( Exception e )
		{
			System.out.println("Fallo al buscar el ultimo indice de una tabla. Probablemente el nombre de la tabla sea incorrecto.");
			System.out.println(sql);
			System.out.println();
			e.printStackTrace();
		}
		disconnect();
		return index;
	}
	
	private void connect()
	{
		try
		{
			if( con==null || con.isClosed() )
			{
				Class.forName("com.mysql.jdbc.Driver");
				con = DriverManager.getConnection("jdbc:mysql://"+url+"/"+dbname,user,passwd);
			}
		}
		catch( Exception e )
		{
			System.out.println("Error conectando a la base de datos:");
			e.printStackTrace();
		}
	}
	
	
	private void disconnect()
	{
		try
		{
			if( con!=null )
				con.close();
		}
		catch( Exception e )
		{
			System.out.println("Error desconectando de la base de datos:");
			e.printStackTrace();
		}
	}

}
